package com.blb.controller;


import com.blb.entity.*;
import com.blb.service.IDeptService;
import com.blb.utils.Response;
import com.blb.utils.UUIDUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.context.request.WebRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *人事管理的部门管理
 * @author xiaoxiong
 * @since 2020-05-20
 */
@Controller
@RequestMapping("/dept")
public class DeptController {


    @Autowired
    private IDeptService iDeptService;

    //跳转到人事管理的部门管理
    @RequiresPermissions("dept")
    @GetMapping("/deptManger")
    public String dept (){
        return "human/depatment";
    }
    @RequiresPermissions("staff")
    //跳转到人事管理的员工管理
    @GetMapping("/Brand_Manage")
    public String Brand_Manage (){
        return "human/Brand_Manage";
    }
    //上级部门添加
    @RequiresPermissions("department:add")
    @PostMapping("/deptHighAdd")
     String add(Dept dept,Model model){
        return iDeptService.addDeptAll(dept,model);
    }

    //下级部门添加
    @PostMapping("/deptLowAdd")
    public String lowAdd(Dept depts,Model model){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String id = depts.getId().toString();
        if(id.equals("")){
            return "human/depatment";
        }
        depts.setId(UUIDUtils.getId());
        depts.setPid(id);
        depts.setCompanyId(user.getId());
        List<Dept> deptList2 = iDeptService.isDeptName(depts.getDeptName(),user.getId());
            if(!deptList2.isEmpty()){
                model.addAttribute("isExist","部门名称已经存在");
                return "human/depatment";
            }else{
                iDeptService.addLowDept(depts);
                return "human/depatment";
            }
    }

    //部门查询
    @ResponseBody
    @RequestMapping("/treeDept")
    public  List<Dept> deptAll(){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<Dept> deptList = iDeptService.deptAll(user.getId());
        return deptList;
    }

    //修改传参
    @RequestMapping("/updTree")
    public String upd(WebRequest req, Model model){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String id =req.getParameter("deptId").trim();
        Dept list = iDeptService.selectById(id,user.getId());
        model.addAttribute("depts",list);
        return "human/depatmentUpdate";
    }

    @PostMapping("/deptHighUpdate")
    public String upd(Dept dept) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        dept.setCompanyId(user.getId());
        iDeptService.updById(dept);
        return "human/depatment";
    }

    @GetMapping("/deptDelete")
    public String del(WebRequest req) {
        String id =req.getParameter("id").trim();
        iDeptService.delDept(id);
        return "human/depatment";
    }

    /**
     * 判断部门名称是否重复
     */
    @ResponseBody
    @GetMapping("/isDeptName")
    Response isDeptName(String deptName){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<Dept> deptList = iDeptService.isDeptName(deptName,user.getId());
        if(deptList.isEmpty()){
            return Response.success();
        }else {
            return Response.error("部门名称重复");
        }

    }

    /**
     * 判断部门编号是否重复
     */
    @ResponseBody
    @GetMapping("/isDeptId")
    Response idDeptId(String deptId){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<Dept> deptList = iDeptService.isDeptId(deptId,user.getId());
        if(deptList.isEmpty()){
            return Response.success();
        }else {
            return Response.error("部门名称重复");
        }

    }

}

